package com.yenlien.traceyun.control.controller;


import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.control.bean.RestURL;
import com.yenlien.traceyun.control.entity.BankInfo;
import com.yenlien.traceyun.control.interceptor.UserAuthUtil;
import com.yenlien.traceyun.control.service.BankInfoService;
import com.yenlien.traceyun.control.service.rest.RestService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 银行信息
 */
@RestController
@RequestMapping("/bankInfo")
public class BankInfoController extends BaseController {

    @Autowired
    private BankInfoService bankInfoSdervice;

    @Autowired
    private RestService restService;

    /**
     * 列表查询
     */
    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody BankInfo bankInfo) {
        return buildPage(bankInfoSdervice.findByAll(bankInfo));
    }

    /**
     * 保存编辑
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(@RequestBody BankInfo bankInfo) {
        BankInfo info = bankInfoSdervice.findByTel(bankInfo.getTel());
        if (info == null) {
            JSONObject json = new JSONObject();
            json.put("bankInfo", bankInfo);
            try {
                JSONObject str = restService.postJson(RestURL.USERCENTER + "user/saveUserAndCompany/", json);
                String companyId = str.getString("companyId");
                bankInfo.setCompanyId(companyId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return success(bankInfoSdervice.save(bankInfo));
    }

    /**
     * 根据id查询
     */
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable("id") String id) {
        return success(bankInfoSdervice.findById(id));
    }

    /**
     * 查询所有总行
     */
    @RequestMapping(value = "/findByBankLevel", method = RequestMethod.GET)
    public Object findByBankLevel() {
        return success(bankInfoSdervice.findByBankLevel());
    }


}
